<?php
App::uses('Sanitize', 'Utility');
class MenuComponent extends Component {
	// the other component your component uses
	public $components = array('Session','FileUpload.FileUpload');

	public $controller = null;

	public function initialize(Controller $controller) {
		$this->controller = $controller;
	}

	public function getMenu($path){
		$template = "subpage";
		$this->controller->loadModel('Menus');
		$this->controller->Menus->recursive = 1;
		$domain_id = $this->Session->read('Config.domain_id');
		if(is_null($domain_id)){
			$this->controller->loadModel('Domain');
			$domain = $this->controller->Domain->get_info(Configure::read('Config.domain'));
			$this->Session->write('Config.domain_id',$domain['Domain']['id']);
			$domain_id = $this->Session->read('Config.domain_id');
		}
		
		$options['joins'] = array(
			array('table' => 'domains_menuses',
				        'alias' => 'DomainsMenus',
				        'type' => 'inner',
				        'conditions' => array(
				            'Menus.id = DomainsMenus.menus_id'
				)
			),
			array('table' => 'domains',
				        'alias' => 'Domain',
				        'type' => 'inner',
				        'conditions' => array(
				            'DomainsMenus.domain_id = Domain.id'
						)
			)
		);
		$options['conditions'] = array(
				    'Domain.id' => $domain_id,
					'Menus.status' => 1
		);
		$options['order'] = array(
						    'Menus.lft'
		);
		$mainMenu = $this->controller->Menus->find('all',$options);

		$pathMenu = null;
		$pathSubmenu = null;
		
		$title_for_layout = "";
		$title_for_content = "";
		$description = "";
		$links = array();
// 		foreach ($submenu as $key => $s) {
// 			$links[$s['Submenu']['slug']] =  array('s'=>$s['Submenu']['id']);
				
// 			$parent = null;
// 			if( $path[0] == $s['Submenu']['slug'] || $path[0] == "index" && $s['Submenu']['slug'] == "/"){
// 				$parent = (isset($s['Menus'][0]['slug'])) ? $s['Menus'][0]['slug'] : null;
// 				if(isset($s['Menus'][0]['id']) && !empty($s['Menus'][0]['id'])){
// 					$title_for_layout = $s['Menus'][0]['name'].' - '.$s['Submenu']['name'];
// 					$title_for_content = $s['Submenu']['name'];
					
// 					$pathMenu = $this->controller->Menus->getPath($s['Menus'][0]['id']);
// 					$pathSubmenu = $this->controller->Submenu->getPath($s['Submenu']['id']);
// 				}
// 				break;
// 			}
// 		}
		
		$subMenu = array();
		$labelTwo = null;
		foreach ($mainMenu as $key => $m) {
			$links[$m['Menus']['slug']] =  array('m'=>$m['Menus']['id']);
			if( isset($path[0]) && $path[0] == $m['Menus']['slug'] || isset($path[0]) && $path[0] == "index" && $m['Menus']['slug'] == "/" || empty($path) &&  $this->controller->request->url == $m['Menus']['slug'] ){
				$title_for_layout = (empty($title_for_layout)) ? Sanitize::html($m['Menus']['name'], array('remove' => true)) : $title_for_layout;
				$title_for_content = (empty($title_for_content)) ? $m['Menus']['name'] : $title_for_content;
				$description = (empty($description)) ? $m['Menus']['description'] : $description;
				$pathMenu = $this->controller->Menus->getPath($m['Menus']['id']);
				$template = (isset($m['Menus']['template']) && !empty($m['Menus']['template'])) ? $m['Menus']['template'] : "subpage";
				Configure::write('Config.template',$template);
				
				$parent_id = ($m['Menus']['parent_id'] == null || $m['Menus']['parent_id'] == 0 ) ? $m['Menus']['id'] : $m['Menus']['parent_id'];
				$parent = $this->controller->Menus->read(null,$pathMenu[0]['Menus']['id']);
				$labelTwo = $this->controller->Menus->find('threaded',array('order'=>'Menus.lft','conditions'=>array('Menus.lft >'=>$parent['Menus']['lft'],'Menus.rght <'=>$parent['Menus']['rght'],'Menus.status' => 1)));
			}
		}

		
		$count = count($path);
		if (!empty($path[$count - 1]) && empty($title_for_layout)) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		
		$options['order'] = 'Menus.lft';
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 0, 'Menus.parent_id' => null,'Menus.status' => 1);
		$mainMenu = $this->controller->Menus->find('all',$options);
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 1, 'Menus.parent_id' => null,'Menus.status' => 1);
		$footerMenu = $this->controller->Menus->find('all',$options);
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 2, 'Menus.status' => 1);
		$leftMenu1 = $this->controller->Menus->find('all',$options);
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 3, 'Menus.status' => 1);
		$leftMenu2 = $this->controller->Menus->find('all',$options);
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 4, 'Menus.status' => 1);
		$leftMenu3 = $this->controller->Menus->find('all',$options);
		
		$options['conditions'] = array('Domain.id' => $domain_id, 'Menus.position' => 5, 'Menus.parent_id' => null,'Menus.status' => 1);
		$rightMenu = $this->controller->Menus->find('all',$options);
		
		$this->controller->set(compact('mainMenu', 'footerMenu', 'leftMenu1', 'leftMenu2', 'leftMenu3', 'rightMenu', 'labelTwo', 'title_for_layout','title_for_content','description','pathMenu','pathSubmenu','template'));
		return $links;
		
	}
	
	public function getSubmenu($recursive = 0 ) {
		
		$this->controller->loadModel('Submenu');
		$this->controller->Submenu->recursive = $recursive;
		$subMenu = $this->controller->Submenu->find('all',array( 'order' => 'lft ASC'));
		return $subMenu;
	}
	
	
	public function getHomepage(){
		
	}
	
	
	public function getFooterMenu($path){
		$this->controller->loadModel('Menus');
		$this->controller->Menus->recursive = 1;
		$domain_id = $this->Session->read('Config.domain_id');
		if(is_null($domain_id)){
			$this->controller->loadModel('Domain');
			$domain = $this->controller->Domain->get_info(Configure::read('Config.domain'));
			$this->Session->write('Config.domain_id',$domain['Domain']['id']);
			$domain_id = $this->Session->read('Config.domain_id');
		}
	
		$options['joins'] = array(
				array('table' => 'domains_menuses',
						'alias' => 'DomainsMenus',
						'type' => 'inner',
						'conditions' => array(
								'Menus.id = DomainsMenus.menus_id'
						)
				),
				array('table' => 'domains',
						'alias' => 'Domain',
						'type' => 'inner',
						'conditions' => array(
								'DomainsMenus.domain_id = Domain.id'
						)
				)
		);
		$options['conditions'] = array(
				'Domain.id' => $domain_id,
				'Menus.position' => 1
		);
		$options['order'] = array(
				'Menus.lft'
		);
		$mainMenu = $this->controller->Menus->find('all',$options);
	
		$this->controller->loadModel('Submenu');
		$this->controller->Submenu->recursive = 1;
	
		$submenu = $this->controller->Submenu->find('all');
	
		$pathMenu = null;
		$pathSubmenu = null;
	
		$title_for_layout = "";
		$title_for_content = "";
	
		$links = array();
		foreach ($submenu as $key => $s) {
			$links[$s['Submenu']['slug']] =  array('s'=>$s['Submenu']['id']);
	
			$parent = null;
			if( $path[0] == $s['Submenu']['slug'] || $path[0] == "index" && $s['Submenu']['slug'] == "/"){
				$parent = (isset($s['Menus'][0]['slug'])) ? $s['Menus'][0]['slug'] : null;
				if(isset($s['Menus'][0]['id']) && !empty($s['Menus'][0]['id'])){
					$title_for_layout = $s['Menus'][0]['name'].' - '.$s['Submenu']['name'];
					$title_for_content = $s['Submenu']['name'];
						
					$pathMenu = $this->controller->Menus->getPath($s['Menus'][0]['id']);
					$pathSubmenu = $this->controller->Submenu->getPath($s['Submenu']['id']);
				}
				break;
			}
		}
	
		$subMenu = array();
		$labelTwo = array();
		foreach ($mainMenu as $key => $m) {
			$links[$m['Menus']['slug']] =  array('m'=>$m['Menus']['id']);
			if( isset($parent) && !is_null($parent) && $parent == $m['Menus']['slug'] || isset($parent) && $parent == "index" && $m['Menus']['slug'] == "/" || $path[0] == $m['Menus']['slug'] || $path[0] == "index" && $m['Menus']['slug'] == "/"){
				$title_for_layout = (empty($title_for_layout)) ? $m['Menus']['name'] : $title_for_layout;
				$title_for_content = (empty($title_for_content)) ? $m['Menus']['name'] : $title_for_content;
				$pathMenu = $this->controller->Menus->getPath($m['Menus']['id']);
	
				$parent_id = ($m['Menus']['parent_id'] == null || $m['Menus']['parent_id'] == 0 ) ? $m['Menus']['id'] : $m['Menus']['parent_id'];
				$labelTwo = $this->controller->Menus->find('all',array('conditions'=>array('Menus.parent_id'=>$parent_id)));
	
				$i = 0;
				foreach ($m['Submenu'] as $skey => $s) {
					$subMenu[$i]['Submenu']= $s;
					$i++;
				}
			}
		}
		$count = count($path);
		if (!empty($path[$count - 1]) && empty($title_for_layout)) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
	
		$options['conditions'] = array(
				'Domain.id' => $domain_id,'Menus.parent_id' => null
		);
		$mainMenu = $this->controller->Menus->find('all',$options);
		$this->controller->set(compact('footerMenu','title_for_layout','title_for_content','pathMenu'));
		return $links;
	
	}
	
}